#!/usr/bin/env ruby

require 'Xml'
require 'Svn'
require 'Pom'
require 'Bpel'
require 'Bpmn'
require 'Mediator'
require 'Spring'
require 'Decision'
require 'Wsdl'


class CompositeSOA < Xml

  attr_accessor :services, :components, :references, :pom

  def domain_name
    if isDoc?
      return doc.xpath("//xmlns:import/@namespace")[0].value.split("/")[3]
    end
    nil
  end

  def components
    if isDoc?
      return @components = doc.xpath('//xmlns:component',doc.root.namespaces)
    end
    nil
  end

  def references
    if isDoc?
      return @references = doc.xpath('//xmlns:reference',doc.root.namespaces)
    end
    nil
  end

  def services
    if isDoc?
      return @services = doc.xpath('//xmlns:service',doc.root.namespaces)
    end
    nil
  end

  def binding_type(node)
      return binding_node(node).name.split('.')[1]
  end

  def binding_node(node)
      return node.at_xpath("#{node.path}//*[starts-with(local-name(),'binding')]")
  end

  def implementation_doc(node)
    node = doc.xpath("//*[local-name()='component'][@name='#{node}']")[0] if node.class == "".class
    type = implementation_type(node)
    src = @path.slice(0,@path.rindex('/')+1) + implementation_node(node).attribute("src").value
    if type == BPMN
      return Bpmn.new(src)
    elsif type == BPEL
      return Bpel.new(src)
    elsif type == MEDIATOR
      return Mediator.new(src)
    elsif type == SPRING
      return Spring.new(src)
    elsif type == DECISION
      return Decision.new(@path.split("/")[0...-2].join("/")+ "/" + Xml.new(src).doc.xpath("//xmlns:path")[0].text)
    else
      return Xml.new(src)
    end
  end

  def implementation_type(node)
      return implementation_node(node).name.split('.')[1]
  end

  def implementation_node(node)
      return node.at_xpath("#{node.path}//*[starts-with(local-name(),'implementation')]")
  end

  def wsdlLocation_abstract(node)
    if isDoc?
      return node.attribute("wsdlLocation").value
    end
    nil
  end

  def location(node)
    if isDoc?
      if binding_type(node) == "jca"
	xml = Xml.new(@path.split("/")[0...-1].join("/")+"/"+binding_node(node)["config"])
	factory = xml.doc.xpath("//@location")[0]
	location = factory.value if !factory.nil?
	destination = xml.doc.xpath("//*[@name='DestinationName']/@value")[0]
	location = location.to_s + "::" + destination.value if !destination.nil?
      else
	location = binding_node(node).attribute("location")
	location = location.value if !location.nil?
      end
      return location
    end
    nil
  end

  def locations
    if isDoc?
      return @locations = references.collect{|r| location(r)}
    end
    nil
  end

  def versionUp(vers = nil)
    if vers.to_s=="" or version.version.to_s.eql? vers.to_s
      version.revision=version.revision.to_i+1
    else
      version.version=vers
    end
    versionNode.content=version.version.to_s
    pom.version=version.version.to_s
    push
    info
  end
  
  def pom
    @pom=Pom.new(@path.split("/")[0...-1].join("/")+"/pom.xml") if @pom==nil
    @pom
  end

  def svn
    @svn=Svn.new @path if @svn==nil
    @svn
  end

  def info
    super.merge("pom" => pom)
  end

  def push
    pom.push
    super
  end

  def createTag
    svn.tag_new(version,nil)
    versionUp
    push
    svn.commit
    info
  end

end

